import re
import xlwt

with open("qq.txt", encoding='utf-8') as f:
    text = f.read()

# data_list = re.findall(r'(\d+\..*?正确答案.*?[A-Z])',text)
data_list = re.findall(r'\[(单选题|多选题)\]<高级>(.*?)\[答案\](.*?)\[结束\]',text)

gen = []
ques = []
ans_a = []
ans_b = []
ans_c = []
ans_d = []
ans_right = []

for i in data_list:
    gen.append(i[0])
    ans_right.append(i[2])
    if i:
        each_topic = re.findall(r'(.*?)A、(.*?)B、(.*?)C、(.*?)D、(.*?)$', i[1])
        print(each_topic)
        if each_topic:
            ques.append(each_topic[0][0])
            ans_a.append(each_topic[0][1])
            ans_b.append(each_topic[0][2])
            ans_c.append(each_topic[0][3])
            ans_d.append(each_topic[0][4])



# 写入Excel
workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('My Worksheet')
for i in range(len(ques)):
    worksheet.write(i, 1, gen[i])
    worksheet.write(i, 2, ques[i])
    worksheet.write(i, 3, ans_a[i])
    worksheet.write(i, 4, ans_b[i])
    worksheet.write(i, 5, ans_c[i])
    worksheet.write(i, 6, ans_d[i])
    worksheet.write(i, 7, ans_right[i])
workbook.save('formatting.xls')
